<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- /导航栏 -->
    <!-- 频道列表 -->
    <!--
animated 滑动的动画
border 底边框线
swipeable 开启左右手势滑动
-->
    <van-tabs class="channel-tabs" v-model="active" swipeable animated border>
      <van-tab :title="item.name" v-for="item in Channels" :key="item.id">
        <artList :channel="item"></artList>
      </van-tab>
      <!-- 右侧自定义内容 -->
      <!-- 占位元素 -->
      <!-- 右侧按钮 -->
      <template #nav-right>
        <div class="placeholder"></div>
        <div class="hamburger-btn" @click="isChannelEditShow = true">
          <i class="iconfont icon-gengduo"></i>
        </div>
      </template>
    </van-tabs>
    <!-- /频道列表 -->
    <van-popup
      class="edit-channel-popup"
      v-model="isChannelEditShow"
      position="bottom"
      :style="{ height: '100%' }"
      closeable
      close-icon-position="top-left"
      ><ChannelEdit
        :myChannels="Channels"
        :active="active"
        @update-active="onUpdateactive"
    /></van-popup>
    <!-- /频道编辑 -->
  </div>
</template>

<script>
import ChannelEdit from "./components/channel-edit.vue";
import { getUserChannels } from "@/api/user";
import artList from "./components/art-list";
import { mapState } from "vuex";
import { getItem } from "@/utils/storage";
export default {
  name: "HomeIndex",
  components: {
    artList,
    ChannelEdit
  },
  props: {},
  data() {
    return {
      isChannelEditShow: false,
      active: 0,
      Channels: []
    };
  },
  computed: {
    ...mapState(["user"])
  },
  watch: {},
  created() {
    this.loadUserChannels();
  },
  mounted() {},
  methods: {
    // async loadUserChannels () {
    //   try {
    //     const { data } = await getUserChannels()
    //     console.log(data)
    //     this.Channels = data.data.channels
    //   } catch (err) {
    //     this.$toast('获取数据失败，请重试')
    //   }
    // }
    // async loadUserChannels () {
    //   try {
    //     const { data } = await getUserChannels()
    //     console.log(data)
    //     this.Channels = data.data.channels
    //   } catch (err) {
    //     this.$toast('获取数据失败，请重试')
    //   }
    // }
    // async loadUserChannels () {
    //   try {
    //     const { data } = await getUserChannels()
    //     console.log(data)
    //     this.Channels = data.data.channels
    //   } catch (err) {
    //     this.$toast('获取数据失败，请重试')
    //   }
    // }
    async loadUserChannels() {
      try {
        // const { data } = await getUserChannels();
        // // console.log(data)
        // this.Channels = data.data.channels;
        let channels = [];
        if (this.user) {
          const { data } = await getUserChannels();
          channels = data.data.channels;
        } else {
          const localChannels = getItem("TOUTIAO_CHANNELS");
          if (localChannels) {
            channels = localChannels;
          } else {
            const { data } = await getUserChannels();
            channels = data.data.channels;
          }
        }

        this.Channels = channels;
      } catch (err) {
        this.$toast("获取数据失败，请重试");
      }
    },
    onUpdateactive(index, isChannelEditShow = true) {
      this.active = index;
      this.isChannelEditShow = isChannelEditShow;
    }
  }
};
</script>

<style scoped lang="less">
.edit-channel-popup {
  box-sizing: border-box;
}
.home-container {
  padding-bottom: 100px;
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 30px;
      color: #777777;
    }
    .van-tabs__wrap {
      padding-top: 90px;
    }

    .van-tab--active {
      color: #333333;
    }

    .van-tabs__nav {
      padding-bottom: 0;
    }

    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }

    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }

    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      background-color: rgba(255, 255, 255, 0.902);
      i.toutiao {
        font-size: 33px;
      }
      &:before {
        content: "";
        position: absolute;
        left: 0;
        width: 1px;
        height: 58px;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
